package net.dgg.rz.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.files.entity.Annex;
import net.dgg.rz.common.files.service.AnnexService;
import net.dgg.rz.common.systemRecord.entity.SystemRecord;
import net.dgg.rz.common.systemRecord.service.SystemRecordService;
import net.dgg.rz.common.utils.DataPermissionUtils;
import net.dgg.rz.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @className UploadController
 * @Author Quanlin Wan
 * @Description //TODO
 * @Date 2019/1/21 9:06
 **/
@Controller
@RequestMapping("/rzsc/upload_file")
public class UploadController extends DggBaseController {
    @Autowired
    AnnexService annexService;
    @Value("${dgg.oss[0].bucket-uri}")
    private String fastdfsHost;
    @Autowired
    SystemRecordService systemRecordService;
    @Autowired
    DataPermissionUtils dataPermissionUtils;

    @RequestMapping("/upload.do")
    @ResponseBody
    public DggRestResponse uploadFile(String fileId, String type,String isOne,String customerId, @RequestParam MultipartFile file) {

        dataPermissionUtils.uoloadFileDataAccess(fileId,null);
        if (null == file) {
            return super.getFailResponse("文件不能为空");
        }
        long fileSize = file.getSize();
        if (fileSize > (20 * 1024 * 1024)) {
            return super.getFailResponse("文件不能超过20MB");
        }
        try {
            if (StringUtils.isEmpty(fileId)) {
                return this.getSuccessResponse("文件自定义Id为空");
            }

            if (StringUtils.isEmpty(customerId)) {
                return this.getSuccessResponse("文件自定义customerId为空");
            }

            if (StringUtils.isEmpty(type)) {
                return this.getSuccessResponse("文件自定义type为空");
            }
            customerId=customerId+"-"+type;
            fileId = customerId + "-" + fileId;


            //如果一个类型的附件，只能上传一个，就先查询原来的附件，然后进行删除
            if (!StringUtils.isEmpty(isOne) && "true".equals(isOne)) {
                List<Annex> files = annexService.findFile(fileId);
                if (null != files) {
                    for (Annex m : files) {
                        //删除原来的附件
                        annexService.delFile(fileId, m.getFilename());
                    }
                }
            }

            //进行上传
            Annex item = annexService.saveMultipartFile(fileId, file);
            /*if (null != item) {
                item.setFilepath(fastdfsHost + item.getFilepath());
            }*/
            // 保存操作日志
            SystemRecord systemRecord = new SystemRecord();
            systemRecord.setSource("5");
            systemRecord.setOperateType("1");
            systemRecord.setWorkId(1L);
            systemRecord.setWorkTableName("annex");
            systemRecord.setRemarks("上传文件：" + item.getFileid() + ":" + item.getFilename());
            systemRecordService.save(systemRecord);
            return super.getSuccessResponse(item);
        } catch (Exception ex) {
            ex.printStackTrace();
            return super.getFailResponse("上传失败(" + ex.getMessage() + ")，请联系管理员");
        }
    }
}
